package
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import utilities.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public final class Animation
	{
		private static var _finger:FlxSprite;
		private static var _fingerTimer:FlxDelay;
		private static var _warningText:FlxText;
		private static var _warningOverlay:FlxSprite;
		private static var _greenBox:BoxHighlight;
		
		public static function init():void
		{
			initFinger();
			initIntruderWarning();
		}
		
		public static function finger(pos:FlxPoint, time:Number = 3):void
		{
			_finger.x = pos.x + 5;
			_finger.y = pos.y - _finger.height - Maze.tileHeight / 2;
			Registry.animations.add(_finger);
			_finger.play("default");
			_fingerTimer.reset(time * 1000);
		}
		
		public static function intruderWarning():void
		{
			if (_warningOverlay == null || _warningText == null)
			{
				initIntruderWarning();
			}
			
			FlxG.flash(0x55550000, 1.5, intruderWarningCallback);
			Registry.animations.add(_warningOverlay);
			Registry.animations.add(_warningText);
		}
		
		public static function greenBox(topLeft:FlxPoint, bottomRight:FlxPoint):void
		{
			_greenBox = new BoxHighlight(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
			_greenBox.addCallback(greenBoxCallback);
			Registry.animations.add(_greenBox);
		}
		
		public static function greenBoxTile(topLeft:FlxPoint):void
		{
			greenBox(topLeft, new FlxPoint(topLeft.x + Maze.tileWidth, topLeft.y + Maze.tileHeight));
		}

		private static function initIntruderWarning():void
		{
			_warningText = new FlxText(0, FlxG.height / 2, FlxG.width, "I N T R U D E R   A L E R T");
			_warningText.size = 40;
			_warningText.alignment = "center";
			_warningText.color = 0xFF000000;
			_warningText.shadow = 0xFFFF0000;
			_warningText.y = FlxG.height / 2 - _warningText.height / 2;
			_warningText.y = _warningText.height / 2;
			
			_warningOverlay = new FlxSprite();
			_warningOverlay.makeGraphic(FlxG.width, 100, 0x55000000);
			_warningOverlay.y = FlxG.height / 2 - _warningOverlay.height / 2;
			_warningOverlay.y = 0;
		}
		
		private static function initFinger():void
		{
			_finger = new FlxSprite();
			_finger.loadGraphic(Assets.ImgFinger, true);
			_finger.width = 1;
			_finger.centerOffsets();
			_finger.addAnimation("default", [0, 1], 6);
			_finger.addAnimationCallback(fingerCallback);
			
			_fingerTimer = new FlxDelay(0);
		}
		
		private static function fingerCallback(animationName:String, frameNumber:uint, frameIndex:uint):void
		{
			if (_fingerTimer.hasExpired)
			{
				Registry.animations.remove(_finger);
			}
		}
		
		private static function greenBoxCallback(box:BoxHighlight):void
		{
			Registry.animations.remove(box, true);
		}
		
		private static function intruderWarningCallback():void
		{
			Registry.animations.remove(_warningOverlay);
			Registry.animations.remove(_warningText);
		}
	}
}